<template>
  <el-dialog
    :title="title"
    :visible="visible"
    :append-to-body="true"
    @close="handleDialogClose"
    width="700px"
  >
    <GenerateForm :data="content" ref="generateForm" />
    <div slot="footer">
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" @click="submitForm">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import WorkOrderApplyService from '../../../../service/work_order_apply'

export default {
  props: {
    show: {
      type: Boolean,
      required: true,
      default: () => false
    },
    data: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      visible: false
    }
  },
  computed: {
    flowKey() {
      return this.data.flowInfo && this.data.flowInfo.flowKey
    },
    title() {
      return this.data.flowInfo && this.data.flowInfo.form.formName
    },
    content() {
      return this.data.flowInfo && JSON.parse(this.data.flowInfo.form.content)
    }
  },
  methods: {
    // 处理Dialog关闭事件
    handleDialogClose() {
      this.$emit('update:show')
      this.$refs.generateForm.reset()
    },
    // 提交表单
    submitForm() {
      this.$refs.generateForm.getData().then(res => {
        const content = JSON.stringify(res)
        WorkOrderApplyService.startProcessInstance(this.flowKey, content).then(
          res => {
            this.$message.success('提交成功')
            this.handleDialogClose()
          }
        )
      })
    }
  },
  watch: {
    show(value) {
      this.visible = value
    }
  }
}
</script>

<style lang="stylus" scoped></style>
